package com.net.xpay.core.helper;

import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.UserProfile;
import com.net.xpay.common.manager.UserProfileManager;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 2020/6/28 10:39
 * 白名单IP验证
 */
@Slf4j
@Component
public class WhiteIpCheckHelper {
    @Autowired
    private UserProfileManager userProfileManager;


    public void checkWhiteIp(User user, String ip) {
       UserProfile userProfile = userProfileManager.getById(user.getId());
       if (CollectionUtils.isEmpty(userProfile.getWhiteIps())) {
           return;
       }
       boolean eq = false;
       for (String whiteIp : userProfile.getWhiteIps()) {
           if (whiteIp.equals(ip)) {
               eq = true;
               break;
           }
       }
       if (!eq) {
           throw new BusinessException(PoseidonErrorCode.INVALID_CLIENT_IP, "当前ip(" + ip + ")不在IP白名单中");
       }
    }


}
